<?php

class Model_Cart
{
	/**
	 * @var Model_CartItem array()
	 */
	protected $_Items = array();
	/**
	 * @var float
	 */
	protected $_SubTotal;
	/**
	 * @var float
	 */
	protected $_ShippingCost;
	/**
	 * @var float
	 */
	protected $_Total;
	/**
	 * @var Zend_Session_Namespace
	 */
	protected $_SessionNamespace;
	
	public function init()
    {
        $this->loadSessionNamespace();
    }
	public function addCartItem($id, $qnty)
	{
		if($qnty < 0)
		{
			return false;
		}
		if ($qnty == 0)
		{
			$this->removeCartItem($id);
			return false;
		}
		
		$item = new Model_CartItem($id, "Books", $qnty);
		$this->_Items[$id] = $item;
		$this->persist();
		
		return $item;
	}
	
	public function removeCartItem($id)
	{
		if(is_int($id))
		{
			unset($this->_Items[$id]);
		}
		
		$this->persist();
	}
	
	public function setSessionNamespace(Zend_Session_Namespace $namespace)
	{
		$this->_SessionNamespace = $namespace;
	}
	
	public function getSessionNamespace()
	{
		if($this->_SessionNamespace === null)
		{
			$this->setSessionNamespace(new Zend_Session_Namespace(__CLASS__));
		}
		return $this->_SessionNamespace;
	}
	
	public function persist()
	{
		$this->getSessionNamespace()->_Items = $this->_Items;
		//$this->getSessionNamespace()->shipping = $this->getShippingCost();
	}
	
	public function loadSessionNamespace()
	{
		if(isset($this->getSessionNamespace()->_Items))
		{
			$this->_Items = $this->getSessionNamespace()->_Items;
		}
	if(isset($this->getSessionNamespace()->_ShippingCost))
		{
			$this->setShippingCost($this->getSessionNamespace()->_ShippingCost);
		}
	}
}